#nullable enable
using Application.Service.Auth;
using Application.Web.Core.Services;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.JSInterop;

namespace Application.Web.Entry.Shared;

public partial class CustomLockScreen : ComponentBase, IDisposable
{
    [Inject] private LockScreen LockScreenService { get; set; } = default!;
    [Inject] private AuthService AuthService { get; set; } = default!;
    [Inject] private IJSRuntime JSRuntime { get; set; } = default!;
    [Inject] private NavigationManager NavigationManager { get; set; } = default!;

    private string _password = string.Empty;
    private bool _isVisible = false;
    private bool _hasError = false;
    private bool _hasEmptyError = false;
    private ElementReference _passwordInput;
    private bool _enabled = Furion.App.GetConfig<bool>("LockScreen:Enabled");

    protected override void OnInitialized()
    {
        if (_enabled)
        {
            LockScreenService.OnLockStateChanged += HandleLockStateChanged;
        }
    }
    
    private async void HandleLockStateChanged(bool isLocked)
    {
        _isVisible = isLocked;
        _hasError = false;
        _hasEmptyError = false;
        await InvokeAsync(StateHasChanged);
        
        // 当锁屏显示时，自动聚焦到密码输入框
        if (_isVisible)
        {
            await Task.Delay(100); // 等待UI渲染完成
            await _passwordInput.FocusAsync();
        }
    }
    
    private async Task UnlockAsync()
    {
        if (string.IsNullOrEmpty(_password))
        {
            _hasEmptyError = true;
            _hasError = false;
            await InvokeAsync(StateHasChanged);
            return;
        }
        
        var result = await LockScreenService.UnlockAsync(_password);
        if (!result)
        {
            _hasError = true;
            _hasEmptyError = false;
            _password = string.Empty;
            await InvokeAsync(StateHasChanged);
            
            // 重新聚焦到密码输入框
            await Task.Delay(50);
            await _passwordInput.FocusAsync();
        }
        else
        {
            _password = string.Empty;
            _hasError = false;
            _hasEmptyError = false;
        }
        
        await InvokeAsync(StateHasChanged);
    }
    
    private async Task LogoutAsync()
    {
        // 清空锁屏计时器
        LockScreenService.ClearInactivityTimer();
        
        // 执行退出登录
        AuthService.Logout();
        
        // 清除本地存储中的锁屏状态
        await JSRuntime.InvokeVoidAsync("localStorage.removeItem", "lockScreenState");
        
        // 隐藏锁屏界面
        _isVisible = false;
        _password = string.Empty;
        _hasError = false;
        _hasEmptyError = false;
        
        // 跳转到登录页面
        NavigationManager.NavigateTo("/login", true);
        
        await InvokeAsync(StateHasChanged);
    }
    
    private async Task HandleKeyPress(KeyboardEventArgs e)
    {
        if (e.Key == "Enter")
        {
            await UnlockAsync();
        }
    }
    
    public void Dispose()
    {
        LockScreenService.OnLockStateChanged -= HandleLockStateChanged;
    }
}